package tech.abing.spider.task.crawler.sac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.crawler.amac.FundAccountSpider.DynamicEntranceImpl;
import tech.abing.spider.task.pageprocessor.sac.SecuritiesCompanyProcessor;
import tech.abing.spider.task.pipeline.sac.SecuritiesCompanyPipeline;
import tech.abing.spider.util.SqlUtil;

public class SecuritiesCompanySpider {
	private static SqlUtil sqlUtil = null;
	
	
	public static void start() {
		CrawlManager securitiesCompany = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = securitiesCompany
		.prepareCrawlTask(DictEnum.TASK_TYPE_SECURITIES_COMPANY.getName(), DefaultPageDownloader.class)
		.useThread(20)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.setPolitenessDelay(200)
		.usePageRetryCount(3)
		.usePipeline(SecuritiesCompanyPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_SECURITIES_COM_INFO.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_SECURITIES_COM_QUA.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_SECURITIES_COM_SAL.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_SECURITIES_COM_SUB.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_SECURITIES_COM_EXE.getCode());
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		securitiesCompany.start(task);
		
	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			String url = "http://jg.sac.net.cn/pages/publicity/resource!search.action";
			
			PageRequest req = context.createPageRequest(url,SecuritiesCompanyProcessor.class);
			
			req.setMethod(Method.POST);
			req.setPostType(PostType.FORMDATA);
			
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Encoding","gzip, deflate");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Connection", "keep-alive");
			req.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			req.putHeader("Cookie","BIGipServerjigou=1095805120.20480.0000; JSESSIONID=XrKcYGrdZN2kFlnG0BwPwJy0Ql0Bv8Mhv1RGzBLQZbQBzJlTpgdh!-1193698414");
			req.putHeader("Host","jg.sac.net.cn");
			req.putHeader("Origin","http://jg.sac.net.cn");
			req.putHeader("Referer","http://jg.sac.net.cn/pages/publicity/securities-list.html");
			req.putHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			
			req.putParams("filter_EQS_O#otc_id","01");
			req.putParams("filter_EQS_O#sac_id","");
			req.putParams("filter_LIKES_aoi_name","");
			req.putParams("sqlkey","publicity");
			req.putParams("sqlval","ORG_BY_TYPE_INFO");
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}
		
	}
}
